package com.white.camlib;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ytf
 * Created by on 2021/06/05 16:46
 */
public class TrackerView2 extends View {

    private float scale = 1.0f;
    private float centerX, centerY;
    private Paint mPaint;
    private List<TrackerDataBundle> datas;

    public TrackerView2(Context context) {
        this(context, null);
    }

    public TrackerView2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TrackerView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        datas = new ArrayList<>();
    }

    public void setScale(float scale, float x, float y) {
        this.scale = scale;
        this.centerX = x;
        this.centerY = y;
    }

    public synchronized void postDatas(List<TrackerDataBundle> d) {
        datas.clear();
        if(d != null && d.size() > 0){
            datas.addAll(d);
        }
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (datas.size() > 0) {
            for (TrackerDataBundle d : datas) {
                RectF rect = d.rect;
                List<PointF> ps = d.points;

                if (rect != null) {
                    if (scale != 1f) {
                        drawRect(canvas, applyScale(rect), d.colorRect);
                    } else {
                        drawRect(canvas, rect, d.colorRect);
                    }
                }
                if (ps != null) {
                    if (scale != 1f) {
                        drawPoint(canvas, applyScale(ps), d.colorPoint);
                    } else {
                        drawPoint(canvas, ps, d.colorPoint);
                    }
                }
            }
        }
    }

    private RectF applyScale(RectF srcRect) {
        RectF dst = new RectF();
        Matrix mx = new Matrix();
        mx.postScale(-1, 1, centerX, 0);
        mx.postScale(scale, scale);
        mx.mapRect(dst, srcRect);
        return dst;
    }

    private List<PointF> applyScale(List<PointF> src) {
        List<PointF> dst = new ArrayList<>();
        float[] srcPoints = new float[src.size() * 2];
        float[] dstPoints = new float[src.size() * 2];
        for (int i = 0; i < src.size(); i++) {
            srcPoints[2 * i] = src.get(i).x;
            srcPoints[2 * i + 1] = src.get(i).y;
        }
        Matrix mx = new Matrix();
        //因为textureview中对人像进行了镜像处理，所以这里也要镜像，否则对应不上
        mx.postScale(-1, 1, centerX, 0);
        mx.postScale(scale, scale);
        mx.mapPoints(dstPoints, srcPoints);
        for (int i = 0; i < src.size(); i++) {
            dst.add(new PointF(dstPoints[2 * i], dstPoints[2 * i + 1]));
        }
        return dst;
    }


    private void drawRect(Canvas canvas, RectF rect, int color) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);
        mPaint.setColor(color);
        canvas.drawRect(rect, mPaint);
//            int delta = 40;
    }

    private void drawPoint(Canvas canvas, List<PointF> points, int color) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(color);
        for (PointF f : points) {
            canvas.drawPoint(f.x, f.y, mPaint);
        }
        if (points.size() >= 5) {
            PointF center = points.get(2);
            mPaint.setStrokeWidth(1);
            PointF lt = points.get(0);
            PointF rt = points.get(1);
            PointF lb = points.get(3);
            PointF rb = points.get(4);
            canvas.drawLine(lt.x, lt.y, center.x, center.y, mPaint);
            canvas.drawLine(rt.x, rt.y, center.x, center.y, mPaint);
            canvas.drawLine(lb.x, lb.y, center.x, center.y, mPaint);
            canvas.drawLine(rb.x, rb.y, center.x, center.y, mPaint);
        }
    }
}
